import {getStat, getUserInfo} from '../../api/member.js';

import Util from '../../utils/util.js';

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: false, // 是否加载中
    loadend: false, // 是否加载完毕
    loadTitle: '加载更多内容', // 提示语
    lists: [],
    page: 1,
    et_show: true,

    statType: [//统计类型
      {title: "今日", id: 1},
      {title: "全程", id: 2},
    ],

    parameter: {
      title: "积分排行版",
      isBack: true,
    },

    rankList: [],
    current_type: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      navH: app.globalData.navHeight,
      activity_id: options.actId || 0,
    });
    const that = this;

    Util.chekWxLogin().then(res => {
      getUserInfo().then(result => {
        that.setData({
          member: result.data || {}
        })

        if (Util.isEmpty(result.data.avatar)) {
          wx.navigateTo({
            url: "../member/edit"
          });
          return;
        }

        that.getLists()
      }).catch(errmsg => {
        app.Tips({title: errmsg})
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  getLists() {
    const that = this

    if (that.data.loadend || that.data.loading) {
      return;
    }

    let filter = that.data.filter || {}
    filter['page_num'] = that.data.page;
    filter['activity_id'] = this.data.activity_id || 0;

    if (this.data.statType[this.data.current_type].id) {
      filter['type'] = this.data.statType[this.data.current_type].id
    }
    if (this.data.sort)
      filter['sort'] = this.data.sort;

    that.setData({loading: true, loadTitle: '正在加载...'});
    let lists = that.data.lists || []
    getStat(filter).then(res => {
      const list = res.data.items || []
      const loadend = res.data.last_page <= that.data.page;

      lists = app.SplitArray(list, lists);

      if (res.data.current_page == 1) {
        let rankList = []
        if (lists.length >= 3) {
          rankList.push(lists[1])
          rankList.push(lists[0])
          rankList.push(lists[2])

          rankList[1]['num'] = 1;
          rankList[0]['num'] = 3;
          rankList[2]['num'] = 2;
        } else if (lists.length === 2) {
          rankList.push(lists[1])
          rankList.push(lists[0])

          rankList[1]['num'] = 2;
          rankList[0]['num'] = 1;
        } else if (lists.length === 1) {
          rankList.push(lists[0])
          rankList[0]['num'] = 1;
        }
        that.setData({
          rankList: rankList
        })
      }

      that.setData({
        lists: lists,
        loadend: loadend,
        loading: false,
        loadTitle: loadend ? '--没有更多了--' : '正在加载...',
        total: res.data.total,
        page: that.data.page + 1,
        et_show: lists.length > 0
      });
    }).catch(errmsg => {
      Util.Tips({title: errmsg})
    }).finally(() => {
      wx.stopPullDownRefresh()
    })
  },

  tapSort(e) {

    let sort = this.data.sort;
    sort = sort == 'asc' ? 'desc' : 'asc';

    this.setData({
      sort: sort,
      loading: false, // 是否加载中
      loadend: false, // 是否加载完毕
      loadTitle: '加载更多内容', // 提示语
      lists: [],
      page: 1,
    })

    this.getLists();
  },

  changeTab(e) {
    this.setData({
      current_type: e.currentTarget.dataset.v,
      loading: false, // 是否加载中
      loadend: false, // 是否加载完毕
      loadTitle: '加载更多内容', // 提示语
      lists: [],
      page: 1,
    })
    this.getLists();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getLists();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})